package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.domain.bo.PtSysQuestionBo;
import com.ruoyi.system.domain.vo.PtSysQuestionVo;
import com.ruoyi.system.domain.PtSysQuestion;
import com.ruoyi.system.mapper.PtSysQuestionMapper;
import com.ruoyi.system.service.IPtSysQuestionService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 实验题目Service业务层处理
 *
 * @author ruoyi
 * @date 2022-03-26
 */
@Service
public class PtSysQuestionServiceImpl extends ServicePlusImpl<PtSysQuestionMapper, PtSysQuestion, PtSysQuestionVo> implements IPtSysQuestionService {

    @Override
    public PtSysQuestionVo queryById(Long id){
        return getVoById(id);
    }

    @Override
    public TableDataInfo<PtSysQuestionVo> queryPageList(PtSysQuestionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PtSysQuestion> lqw = buildQueryWrapper(bo);
        Page<PtSysQuestionVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public List<PtSysQuestionVo> queryList(PtSysQuestionBo bo) {
        LambdaQueryWrapper<PtSysQuestion> lqw = buildQueryWrapper(bo);
        return listVo(lqw);
    }

    private LambdaQueryWrapper<PtSysQuestion> buildQueryWrapper(PtSysQuestionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PtSysQuestion> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExpId() != null, PtSysQuestion::getExpId, bo.getExpId());
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), PtSysQuestion::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), PtSysQuestion::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getOptionA()), PtSysQuestion::getOptionA, bo.getOptionA());
        lqw.eq(StringUtils.isNotBlank(bo.getOptionB()), PtSysQuestion::getOptionB, bo.getOptionB());
        lqw.eq(StringUtils.isNotBlank(bo.getOptionC()), PtSysQuestion::getOptionC, bo.getOptionC());
        lqw.eq(StringUtils.isNotBlank(bo.getOptionD()), PtSysQuestion::getOptionD, bo.getOptionD());
        lqw.eq(StringUtils.isNotBlank(bo.getAnswer()), PtSysQuestion::getAnswer, bo.getAnswer());
        lqw.eq(StringUtils.isNotBlank(bo.getAnalysis()), PtSysQuestion::getAnalysis, bo.getAnalysis());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), PtSysQuestion::getStatus, bo.getStatus());
        lqw.eq(bo.getScore() != null, PtSysQuestion::getScore, bo.getScore());
        return lqw;
    }

    @Override
    public Boolean insertByBo(PtSysQuestionBo bo) {
        PtSysQuestion add = BeanUtil.toBean(bo, PtSysQuestion.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(PtSysQuestionBo bo) {
        PtSysQuestion update = BeanUtil.toBean(bo, PtSysQuestion.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(PtSysQuestion entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
